package com.codegenerate.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codegenerate.demo.config.R;
import com.codegenerate.demo.dto.SysAccountDto;
import com.codegenerate.demo.entity.SysAccount;
import com.codegenerate.demo.service.impl.SysAccountServiceImpl;
import com.codegenerate.demo.utils.StringUtils;

import cn.hutool.log.LogFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;


@RestController
@RequestMapping("/sys_account")
public class SysAccountController {

	Logger logger = LoggerFactory.getLogger(SysAccountController.class);
	
    @Autowired
    SysAccountServiceImpl sysAccountService;


    @PostMapping("/list")
    R<List<SysAccount>> list(@RequestBody SysAccountDto sysAccountDto){
        Page<SysAccount> page = sysAccountService.page(
                sysAccountDto.buildPage(),sysAccountDto.dto2entity().getQueryAll()
        );
        return R.ok(page.getRecords())
                .setCount(page.getTotal());
    }

    @GetMapping("/getItem")
    R<SysAccount> getItem(int id) {
        return R.ok(sysAccountService.getById(id));
    }

    @PostMapping("/add")
    R<Boolean> add(@RequestBody  SysAccountDto sysAccountDto){
        var entity=sysAccountDto.dto2entity();
        // region  设置 默认
            SysAccount.setDefaultValue(entity);
            //设置默认密码
            SysAccount.setDefaultPassword(entity);
        // endregion
        return R.ok(sysAccountService.save(entity));
    }


    @PostMapping("/update")
    R<Integer> update(@RequestBody  SysAccountDto sysAccountDto){
        sysAccountDto.validateUpdate();
        return R.ok(sysAccountService.getBaseMapper().update(null, sysAccountDto.dto2entity().getUpdateWrapper()));
    }

    @GetMapping("/del")
    R<Boolean> del(@RequestParam("id") int id) {
        return R.ok(sysAccountService.removeById(id));
    }
    @GetMapping("/hello")
    R<String> hello(@RequestParam("name")String name){
    	logger.info("我就是一个测试===》"+name);
    	return R.ok(StringUtils.format("name 11 is  {}",name));
    }
    
}




